package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import po.Client;
import po.Dish;
import po.Merchant;
import po.OrderItems;
import po.Orders;
import service.DishService;
import service.OrdersService;
import util.Po2Vo;
import vo.OrdersVo;
import vo.ResponseEntity;

@Controller
public class ViewOrdersController {
	@Autowired
	private OrdersService ordersService; 
	@Autowired
	private DishService dishService;
	
	@RequestMapping("/waitingForReceiving")
	public String waitingForReceiving(Model model,HttpSession session){
		Client client=(Client)session.getAttribute("client");
		List<OrdersVo> waitingForReceivingOrders=ordersService.findWaitingForReceiveOrdersVoByCid(client.getCid());
		List<String> strings=ordersService.findWaitingForPickOrdersOidsByCid(client.getCid());
		session.setAttribute("oids", strings);
		model.addAttribute("waitingForReceivingOrders", waitingForReceivingOrders);
		return "waitingForReceiving";
	}
	
	@RequestMapping("/historicalOrders")
	public String historicalOrders(Model model,HttpSession session){
		Client client=(Client) session.getAttribute("client");
		List<OrdersVo> historicalOrders=ordersService.findHistoricalOrdersVoByCid(client.getCid());
		model.addAttribute("historicalOrders", historicalOrders);
		return "historicalOrders";
	}
	
	@RequestMapping("/orederDetails")
	public String orderDetails(String oid,Model model){
		Orders order=ordersService.findOrder(oid);
		OrdersVo ordersVo=Po2Vo.ordersPo2Vo(order);
		model.addAttribute("ordersVo", ordersVo);
		return "orderDetails";
	}	
	
	@RequestMapping("/evaluateOrders")
	public String evaluateOrders(HttpSession session,String oid){
		OrdersVo orders=ordersService.findOrderVo(oid);
		session.setAttribute("order", orders);
		return "gradeAndcomplain";
	}
	
	@ResponseBody
	@RequestMapping("/refreshOrderDetails")
	public String refreshOrderDetails(HttpSession session){
		List<String> strings=(List<String>) session.getAttribute("oids");
		if(strings!=null){
			for (String string : strings) {
				OrdersVo orders=ordersService.findOrderVo(string);
				
				if(orders.getStatusInt()==2){
					strings.remove(string);
					session.setAttribute("oids", strings);
					return orders.getoId()+",refuse";
				}
				else if(orders.getStatusInt()==1){{
					strings.remove(string);
					session.setAttribute("oids", strings);
					return orders.getoId()+",accept";
				}
				}
			}			
		}
		return "nochange";
	}
	@ResponseBody
	@RequestMapping("/confirmReceiveOrder")
	public ResponseEntity confirmOrder(String oid){
		ResponseEntity re = new ResponseEntity();
		Orders orders=ordersService.findOrder(oid);
		orders.setReceive(1);
		ordersService.updateOrdersRecieve(orders);
		re.setStatus(true);
		re.setMessage("确认收货完成");
		re.setData(oid);
		return re;
	}
	
	@RequestMapping("/addOrders/{mid}")
	public ModelAndView addOrders(@PathVariable String mid,HttpSession session){
		ModelAndView mv = new ModelAndView();
		Map<String,Map<String,Integer>> carts = (Map<String,Map<String,Integer>>) session.getAttribute("carts");
		Client client = (Client) session.getAttribute("client");
		List<OrderItems> orderItems = new ArrayList<OrderItems>();
		OrderItems item = null;
		Orders order = new Orders();
		if(client != null && carts != null && carts.containsKey(mid) ){
			Merchant merchant = new Merchant();
			merchant.setmId(mid);
			Map<String,Integer> dishList = carts.get(mid);
			for(String id:dishList.keySet()){
				item = new OrderItems();
				Dish d = dishService.findDishByDid(id);
				item.setDid(id);
				item.setdName(d.getdName());
				item.setImg(d.getImg());
				item.setNum(dishList.get(id));
				item.setPrice(d.getPrice());
				orderItems.add(item);
			}
			order.setClient(client);
			order.setMerchant(merchant);
			order.setDt(new Date());
			order.setOrderItems(orderItems);
			order.setStatus(0);
			order.setOgrade(-1);
			order.setReceive(0);
			order.setComplain("");
			ordersService.addOrder(order);
			carts.remove(mid);
			mv.setViewName("redirect:/waitingForReceiving");
		}else if(!carts.containsKey(mid)){
			mv.setViewName("redirect:/merchant/" + mid);
		}
		return mv;
	}
	
	@RequestMapping("confirmOrder/{mid}")
	public ModelAndView confirmOrder(HttpServletRequest request,HttpSession session,@PathVariable String mid){
		ModelAndView mv = new ModelAndView();
		Map<String,Map<String,Integer>> carts = (Map<String,Map<String,Integer>>) session.getAttribute("carts");
		Client client = (Client) session.getAttribute("client");
		List<OrderItems> orderItems = new ArrayList<OrderItems>();
		OrderItems item = null;
		Orders order = new Orders();
		if(client != null && carts != null && carts.containsKey(mid) && !carts.get(mid).isEmpty()){
			Merchant merchant = new Merchant();
			merchant.setmId(mid);
			Map<String,Integer> dishList = carts.get(mid);
			for(String id:dishList.keySet()){
				item = new OrderItems();
				Dish d = dishService.findDishByDid(id);
				item.setDid(id);
				item.setdName(d.getdName());
				item.setImg(d.getImg());
				item.setNum(dishList.get(id));
				item.setPrice(d.getPrice());
				orderItems.add(item);
			}
			order.setClient(client);
			order.setMerchant(merchant);
			order.setDt(new Date());
			order.setOrderItems(orderItems);
			order.setStatus(0);
			order.setOgrade(3);
			order.setReceive(0);
			order.setComplain("===");
			mv.addObject("order", order);
			mv.setViewName("confirmOrder");
		}else {
			mv.setViewName("redirect:/merchant/" + mid);
		}
		return mv;
	}
}
